package com.android.diary;

import java.util.Calendar;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.PowerManager;
import android.util.Log;

public class Diary {

	private final Context context;
	SQLiteDatabase Db;
	DbHelper dbHelper;
	public static final String key_id="_id";
	public static final String key_title="title";
	public static final String key_content="content";
	public static final String key_created="created";
	private static final String DATABASE_NAME = "db_UserInfo";
	private static final String DATATABLE_NAME = "mydiary";
	private static final int DATABASE_VERSION = 2;
	private static final String DATABASE_CREATE = "create table mydiary (_id integer primary key autoincrement," +
			"title text not null,content text not null,created text not null);";

	private static class DbHelper extends SQLiteOpenHelper {
		DbHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
			Log.i("create :"," ok");
		}
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS mydiary");
			onCreate(db);
		}
	}

	public Diary(Context context) {
		this.context = context;
	}
	public Diary open()throws SQLException{
		dbHelper=new DbHelper(context);
		Db =dbHelper.getWritableDatabase();
		return this;
	}
	public void closeDB(){
		dbHelper.close();
	}
	public void deleteTable(){
		Db.execSQL("DROP TABLE mydiary");
	}
	public long createDiary(String title,String content){
		ContentValues contentValues=getValue(title,content);
		return Db.insert(DATATABLE_NAME,null,contentValues);
	}
	public ContentValues getValue(String title,String content){
		ContentValues contentValues=new ContentValues();
		contentValues.put(key_title, title);
		contentValues.put(key_content,content);
		Calendar calendar=Calendar.getInstance();
		String created=calendar.get(Calendar.YEAR)+"年"
		+(calendar.get(Calendar.MONTH)+1)+"月"
		+calendar.get(Calendar.DAY_OF_MONTH)+"日"
		+(calendar.get(Calendar.HOUR_OF_DAY)+8)+"小时"
		+calendar.get(Calendar.MINUTE)+"分"
		+calendar.get(Calendar.SECOND)+"秒";
		contentValues.put(key_created,created);
		return contentValues;
	}
	//�����־
	public boolean updateDiary(String title,String content,long rowId){
		ContentValues contentValues=getValue(title,content);
		return Db.update(DATATABLE_NAME,contentValues,key_id + "=" + rowId,null)>0;
		
	}
	//ɾ����־
	public boolean deleteDiary(long rowId){
		return Db.delete(DATATABLE_NAME,key_id+"="+rowId,null)>0;
	}
	//��ȡ������־�б�
	public Cursor getAllDiary(){
		Log.i("setcontentview","setcontentview");
		return Db.query(DATATABLE_NAME,new String[]{key_id, key_title,key_content,key_created},
				null,null,null,null, null);
	}
	//��ȡָ����־�б�
	public Cursor getDairy(long rowId){
		Cursor cursor=Db.query(true,DATATABLE_NAME,new String[]{key_id,key_title,key_content,key_created},
				key_id+"="+rowId, null, null, null, null, null);
		if(cursor!=null){
			cursor.moveToFirst();
		}
		return cursor;
	}
}
